<template>
  <BasicModal :show="show" title="编辑" width="30%" :widthComfirm="true" @comfirm="comfirm"
              @closeModal="show = false">
    <template #content>
      <CommonForm labelWidth="110px" :formList="formList" ref="formRef"></CommonForm>
    </template>
  </BasicModal>
</template>

<script setup lang='ts'>
import { ref } from "vue"
import CommonForm from "@/views/ApplicationDetails/CommonForm.vue";
import BasicModal from "@/views/ApplicationDetails/Modals/BasicModal.vue"
import {
  updateUniversityTarget,
} from '@/api/data-center/university-data.ts'
import { message } from "ant-design-vue";

const props = defineProps(['actives'])
const emit = defineEmits(['finishEditing'])

// 弹窗显示状态
const show = ref(false);

// 表单配置
const formList = ref([
  { label: "高校名称", name: "universityName", value: "", component: 'input', },
  { label: "所属大区", name: "deptName", value: "", component: 'select', options: [] },
  { label: "高校负责人", name: "userName", value: "", component: 'select',options: []},
  { label: "应届本科生数", name: "currentStudent", value: "", component: 'input',attrs:{type:"number"} },
  { label: "招聘目标", name: "target", value: "", component: 'input',attrs:{type:"number"} },
])

// 表单组件绑定ref
const formRef = ref(null)

// 传递来的表单参数
const itemParams = ref(null)

/**
 * 弹窗打开事件
 * @param params
 */
const open = (params) => {
  itemParams.value = params
  formList.value[1].options = params.regions
  formList.value[2].options = params.dutyNames
  formList.value[0].value = params.record.universityName
  formList.value[1].value = params.record.region
  formList.value[2].value = params.record.channelManager
  formList.value[3].value = params.record.undergraduateCount
  formList.value[4].value = params.record.target
  show.value = true
}

/**
 * 弹窗确认事件
 */
const comfirm = async () => {
  const fromData = await formRef.value.submitForm();
  const params = {
    id: itemParams.value.record.id,
  }
  fromData.forEach(item=>{
    params[item.name] = item.value
  })
  const res = await updateUniversityTarget(params);
  if (res==='成功') {
    message.success("编辑成功");
    show.value = false;
    emit("finishEditing")
  } else {
    message.error(res.msg)
  }
}

defineExpose({ open })
</script>

